#!/usr/bin/env python 
# -*- coding: utf-8 -*-
# @Time    : 2022/10/24 8:45
# @Author  : Junzhao
# @File    : 系统.py
# @Software: PyCharm
import os.path
import pickle
import sys
from 作业.学生管理系统.面向对象.学生 import Student


class SMS(object):

    def __init__(self, _students=[]):
        with open("SMS.pkl", 'rb')as f:
            """将文件中已有的队形读入内存"""
            if os.path.getsize("SMS.pkl") != 0:
                self.students = pickle.load(f)
            else:
                self.students = _students
            f.close()

        self._main_menu()
        pass

    def _main_menu(self):
        """主菜单"""
        try:
            print("=======欢迎使用学生管理系统 v2.0=======")
            print("------------------------------------")
            _ = int(input("请输入功能对应的序号：\n"
                          "1.增加学生信息\n"
                          "2.删除学生信息\n"
                          "3.修改学生信息\n"
                          "4.查找学生（通过id）\n"
                          "5.显示所有学生信息\n"
                          "6.退出系统\n"
                          "===================================="))
            if _ == 1:
                self._insert()
            elif _ == 2:
                self._delete()
            elif _ == 3:
                self._modify()
            elif _ == 4:
                self._search()
            elif _ == 5:
                self._total()
            elif _ == 6:
                self._quit()
        except ValueError:
            print("请检查你的输入")
        finally:
            self._main_menu()
        pass

    def _insert(self):
        try:
            _s = Student(int(input('请输入学生学号')),
                         input('请输入学生姓名'),
                         float(input('请输入学生英语成绩')),
                         float(input('请输入学生Python成绩')),
                         float(input('请输入学生Java成绩')))
            if _s not in self.students:
                self.students.append(_s)
                print("信息添加成功！")
            else:
                print("该学生已存在！请检查")
        except ValueError:
            print("请检查你的输入")
        finally:
            quit = input("输入除 q 外任何字母继续添加")
            if quit == 'q':
                self._main_menu()
            else:
                self._insert()
        pass

    def _search(self):
        if not self.students:
            print("还未有学生信息！")
            self._main_menu()
        else:
            try:
                _ = int(input("请输入学号"))
                for i in self.students:
                    if i.id == _:
                        print("找到该学生，信息为：")
                        i.show()
                        self._main_menu()
                print("未找到该学生！")
                self._main_menu()
            except ValueError:
                print("请检查你的输入")
            finally:
                quit = input("输入除 q 外任何字母继续查找")
                if quit == 'q':
                    self._main_menu()
                else:
                    self._search()


        pass

    def _delete(self):
        if not self.students:
            print("还未有学生信息！")
            self._main_menu()
        else:
            try:
                _ = int(input("请输入学号"))
                for i in self.students:
                    if i.id == _:
                        self.students.remove(i)
                        print("删除成功")
                        self._main_menu()

                print("未找到该学生")
                self._main_menu()
            except ValueError:
                print("请检查你的输入")
            finally:
                quit = input("输入除 q 外任何字母继续删除")
                if quit == 'q':
                    self._main_menu()
                else:
                    self._delete()

        pass

    def _modify(self):
        if not self.students:
            print("还未有学生信息！")
            self._main_menu()
        else:
            try:
                _ = int(input("请输入学号"))
                for i in self.students:
                    if i.id == _:
                        id = i.id
                        self.students.remove(i)
                        i = Student(id,
                                    input("请输入新的姓名"),
                                    float(input("请输入英语成绩")),
                                    float(input("请输入Python成绩")),
                                    float(input("请输入JAVA成绩")))
                        self.students.append(i)
                        print("修改成功")
                        self._main_menu()
                    else:
                        print("未找到该学生")
                        self._main_menu()
            except ValueError:
                print("请检查你的输入")
            finally:
                quit = input("输入除 q 外任何字母继续修改")
                if quit == 'q':
                    self._main_menu()
                else:
                    self._modify()
        pass

    def _total(self):
        if self.students is not None:
            for _ in self.students:
                _.show()
                # print()
            self._main_menu()
        else:
            print("还未有学生信息！")
        pass

    def _quit(self):
        with open("SMS.pkl", 'wb')as f:
            pickle.dump(self.students, f)
        sys.exit()
        pass


if __name__ == '__main__':
    sms = SMS()
